package com.bl.weilan.api.controller;

import com.bl.weilan.common.ApiResponseBody;
import com.bl.weilan.common.enums.WechatBusinessType;
import com.bl.weilan.common.utils.CommonUtils;
import com.bl.weilan.service.biz.main.MainService;
import com.bl.weilan.service.biz.message.MessageService;
import com.bl.weilan.service.vo.main.*;
import io.swagger.annotations.*;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

/**
 * Created by liangweikang on 2018/1/17.
 */
@RestController
@Log4j2
@RequestMapping(value = {"/weilan/apis"})
@Api(value = "注册登陆相关api", description = "注册登陆相关api")
public class MainController extends BaseController {

    static final private String OPEN_ID_COOKIE_NAME = "OPEN_ID";
    static final private String MOBILE_COOKIE_NAME = "MOBILE";
    static final private String UNION_ID_COOKIE_NAME = "UNION_ID";
    static final private String USER_ID_COOKIE_NAME = "USER_ID";

    @Autowired
    MainService mainService;

    @Autowired
    MessageService messageService;

    @ApiOperation(value = "获取短信验证码", notes = "获取短信验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", value = "手机号", required = true, paramType = "query", dataType = "String")})
    @GetMapping(value = {"/get_captcha"})
    public ApiResponseBody getCaptcha(
            @RequestParam(value = "mobile", required = true) String mobile,
            HttpServletRequest request) {

        messageService.sendCaptchaMessage(mobile);

        return ApiResponseBody.createSuccessBody(null);
    }

    @ApiOperation(value = "登陆", notes = "sid不同时会重新登陆")
    @PostMapping(value = {"/login"})
    public ApiResponseBody<LoginResponseVo> login(
            @Valid @RequestBody LoginVo body, BindingResult result,
            HttpServletRequest request, HttpServletResponse response) {

        ApiResponseBody validResult = validParams(result);
        if (validResult != null) {
            return validResult;
        }

        LoginResponseVo loginResponseVo = mainService.appLogin(body);

        // set cookie
        Cookie tokenCookie = new Cookie(MOBILE_COOKIE_NAME, loginResponseVo.getMobile());
        tokenCookie.setPath("/");
        tokenCookie.setMaxAge(3600 * 24 * 300);
        Cookie userIdCookie = new Cookie(USER_ID_COOKIE_NAME, loginResponseVo.getId());
        userIdCookie.setPath("/");

        response.addCookie(tokenCookie);
        response.addCookie(userIdCookie);

        return ApiResponseBody.createSuccessBody(loginResponseVo);
    }

    @ApiOperation(value = "修改密码", notes = "")
    @PostMapping(value = {"/reset"})
    public ApiResponseBody reset(
            @Valid @RequestBody ResetVo body, BindingResult result,
            HttpServletRequest request, HttpServletResponse response) {
        ApiResponseBody validResult = validParams(result);
        if (validResult != null) {
            return validResult;
        }
        mainService.reset(body);
        return ApiResponseBody.createSuccessBody(null);
    }

    @ApiOperation(value = "忘记密码", notes = "")
    @PostMapping(value = {"/forget"})
    public ApiResponseBody forget(
            @Valid @RequestBody ForgetVo body, BindingResult result,
            HttpServletRequest request, HttpServletResponse response) {
        ApiResponseBody validResult = validParams(result);
        if (validResult != null) {
            return validResult;
        }
        mainService.forget(body);
        return ApiResponseBody.createSuccessBody(null);
    }



    @ApiOperation(value = "登出", notes = "")
    @ApiImplicitParams({
    })
    @GetMapping(value = {"/logout"})
    public ApiResponseBody logout(
            HttpServletRequest request, HttpServletResponse response) {
        mainService.logout(getUserInfo(request));
        return ApiResponseBody.createSuccessBody(null);
    }

    @ApiOperation(value = "注册接口", notes = "")
    @PostMapping(value = {"/register"})
    @ResponseBody
    public ApiResponseBody register(
            @Valid @RequestBody RegisterVo body, BindingResult result,
            HttpServletRequest request, HttpServletResponse response) {

        ApiResponseBody validResult = validParams(result);
        if (validResult != null) {
            return validResult;
        }

        LoginResponseVo loginResponseVo = mainService.register(body);

        // set cookie
        Cookie tokenCookie = new Cookie(MOBILE_COOKIE_NAME, loginResponseVo.getMobile());
        tokenCookie.setPath("/");
        Cookie userIdCookie = new Cookie(USER_ID_COOKIE_NAME, loginResponseVo.getId());
        userIdCookie.setPath("/");

        response.addCookie(tokenCookie);
        response.addCookie(userIdCookie);

        return ApiResponseBody.createSuccessBody(loginResponseVo);
    }


}
